package com.pet.shop.controller.product;

import com.pet.shop.controller.BaseController;
import com.pet.shop.entity.product.Product;
import com.pet.shop.entity.product.ProductType;
import com.pet.shop.service.IBaseService;
import com.pet.shop.service.product.IProductService;
import com.pet.shop.service.product.IProductTypeService;
import com.pet.shop.utils.ApiResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("/web/product")
public class ProductController extends BaseController<Product>{

    @Autowired
    private IProductService productService;
    @Autowired
    private IProductTypeService productTypeService;
    @Value("${fileViewServer}")
    private String fileViewServer;
    @Value("${fileContextPath}")
    private String fileContextPath;

    @Override
    public IBaseService<Product> getBaseService() {
        return productService;
    }

    @RequestMapping("list")
    public ModelAndView list(@RequestParam(required = true, defaultValue = "1") Integer viewType){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("viewType", viewType);
        List<ProductType> productTypeList = productTypeService.queryProductTypeList(paramMap);
        return new ModelAndView("shopping/productList").addObject("typeList", productTypeList)
                .addObject("viewType", viewType).addObject("fileViewServer", fileViewServer)
                .addObject("brandShow", new Integer(1).equals(viewType)).addObject("fileContextPath", fileContextPath);
    }

    @RequestMapping("service/{typeId}")
    public ModelAndView service(@PathVariable("typeId") Long typeId){
        ProductType productType = productTypeService.getEntity(typeId);
        return new ModelAndView("shopping/serviceList").addObject("fileViewServer", fileViewServer)
                .addObject("productType", productType);
    }

    @RequestMapping("updateRemark")
    @ResponseBody
    public Map<String, Object> updateRemark(HttpServletRequest request, Product product){
        if(product == null || product.getId() == null){
            return ApiResultUtil.error_param("参数不能为空");
        }
        product.setModifierId(getSessionUser(request).getId());
        product.setModifyTime(new Date());
        productService.updateProductRemark(product);
        return ApiResultUtil.success("ok");
    }
}
